package com.snap.vseries.model;

import com.snap.vseries.bean.ExpEntity;

import org.litepal.crud.callback.CountCallback;
import org.litepal.crud.callback.FindCallback;
import org.litepal.crud.callback.FindMultiCallback;
import org.litepal.crud.callback.SaveCallback;
import org.litepal.crud.callback.UpdateOrDeleteCallback;

import java.util.List;

public class DBManager {

    private static final String TAG = "DBManager";
    private static final Object OBJECT = new Object();
    private static DBManager dbManager;
    private DBModel dbModel;

    private DBManager() {
        dbModel = new DBModel();
    }

    public synchronized static DBManager get() {
        if (dbManager == null) {
            synchronized (OBJECT) {
                if (null == dbManager) {
                    dbManager = new DBManager();
                }
            }
        }
        return dbManager;
    }

    //=============================👇同步方法👇==================================
    //保存模板 同步！！！
    public boolean saveDemoSync(ExpEntity demo) {
        return dbModel.saveDemoSync(demo);
    }

    //保存实验 同步！！！
    public boolean saveExpSync(ExpEntity experiment) {
        return dbModel.saveExpSync(experiment);
    }

    //=============================👇预设模板👇==================================
    //获取预设模板
    public void getTemplateList(FindMultiCallback<ExpEntity> callback) {
        dbModel.getTemplateList(callback);
    }

    //获取预设模板
    public List<ExpEntity> getTemplateList() {
        return dbModel.getTemplateList();
    }

    //保存模板 异步！！！
    public void saveTemplate(ExpEntity experiment, SaveCallback callback) {
        dbModel.saveTemplate(experiment, callback);
    }

    //保存模板 同步！！！
    public boolean saveTemplate(ExpEntity experiment) {
        return dbModel.saveTemplate(experiment);
    }

    public boolean saveTemplateList(List<ExpEntity> expEntities) {
        return dbModel.saveTemplateList(expEntities);
    }

    //=============================👇实验👇==================================
    //保存实验
    public void saveExp(ExpEntity experiment, SaveCallback callback) {
        dbModel.saveExp(experiment, callback);
    }

    //获取全部实验列表
    public void getExp(FindMultiCallback<ExpEntity> callback) {
        dbModel.getExpList(Integer.MAX_VALUE, callback);
    }

    //收藏实验
    public void collectExp(UpdateOrDeleteCallback callback, String... name) {
        dbModel.collectExp(callback, name);
    }

    //删除实验
    public void deleteExp(UpdateOrDeleteCallback callback, String... name) {
        dbModel.deleteExp(callback, name);
    }

    //删除实验
    public void deleteTemplate(UpdateOrDeleteCallback callback, String... name) {
        dbModel.deleteTemplate(callback, name);
    }

    //删除实验
    public void deleteTemplate(String... name) {
        dbModel.deleteTemplate(name);
    }
    //多条件查询实验数目
    public void getExpCount(String name, CountCallback callback) {
        dbModel.getExpCount(name, callback);
    }

    /**
     * 根据实验名称获取实验列表
     */
    public List<ExpEntity> getExpByNameSync(String name) {
        return getExpSync(name, 0, Integer.MAX_VALUE);
    }

    /**
     * 获取limit条实验记录
     */
    public List<ExpEntity> getExpByNameSync(int limit) {
        return getExpSync("", 0, limit);
    }

    //多条件查询实验（分页）
    public List<ExpEntity> getExpSync(String name, int offset, int limit) {
        List<ExpEntity> expEntities = dbModel.getExpSync(name, offset, limit);
        return expEntities;
    }

    //多条件查询实验（分页）
    public void getExp(String name, int offset, int limit, FindMultiCallback<ExpEntity> callback) {
        dbModel.getExp(name, offset, limit, callback);
    }

    //获取所有实验
    public void getAllExp(FindMultiCallback<ExpEntity> callback) {
        dbModel.getAllExp(callback);
    }

    //根据名称查找实验
    public void getExpByName(String name, FindCallback<ExpEntity> callback) {
        dbModel.getExpByName(name, callback);
    }

    //根据名称查找模板
    public void getTemplateByName(String name, FindCallback<ExpEntity> callback) {
        dbModel.getTemplateByName(name, callback);
    }
}
